package com.dns.customers.filter;
  
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.filter.OncePerRequestFilter;
  
/**
 * 登录过滤
 *
 */
public class CheckLoginFilter extends OncePerRequestFilter {
  
    @Override
    protected void doFilterInternal(HttpServletRequest request,
            HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        //对request和response进行一些预处理
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        System.out.println("这里是过滤器。。。。。。。。。。。。。。。。。。。。。。。");
        System.out.println(request.getRequestURL());
        System.out.println(request.getRequestURI());
        System.out.println(request.getContextPath());
        System.out.println(request.getServletPath());
        System.out.println(request.getQueryString());
        String url = request.getRequestURI();
        int last = url.lastIndexOf("/");

        String urlsp = url.substring(last,url.length());
        System.out.println("urlsp:"+urlsp);
        //如果不是登录请求,就验证是否登录
        if (!"/login".equals(urlsp)){
            Object user = request.getSession().getAttribute("user");
            //验证是否登录，没有登录的就重定向到登录页面
            if (user == null){
                String ul = request.getScheme() + "://"
                        + request.getServerName() + ":" + request.getServerPort()+request.getContextPath();
                response.sendRedirect(ul);
            }else {
                filterChain.doFilter(request,response);
            }
        }else {
            filterChain.doFilter(request,response);
        }
    }
}